﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace fgbt_move
{
    class disk
    {
        String drivename;
        DriveInfo drive;
        long free;

        public disk(String name)
        {
            drivename = name;
            drive = new DriveInfo(name);
            free = drive.AvailableFreeSpace;
        }

        public String getname()
        {
            return drivename + @":\";
        }

        public long getFreeSize()
        {
            free = drive.AvailableFreeSpace;
            return free;
        }

        public void clean(long need)
        {
            System.Console.WriteLine("准备磁盘空间:" + (need - 10000000000) / 1024 / 1024 + "MB\n");
            String ca_name;
            long ca_time;
            int num = 0;
            free = drive.AvailableFreeSpace;
            while (free < need)
            {
                //=====================================遍历过程==============================
                String[] FileName = null;
                long[] Crt_File = null;
                FileName = Directory.GetFileSystemEntries(drive.RootDirectory.ToString());
                Crt_File = new long[FileName.Length];
                for (int i = 0; i < FileName.Length; i++)
                {
                    Crt_File[i] = IsNumeric(Directory.GetCreationTime(FileName[i]).ToString("yyyyMMddHHmmss"));
                }

                //===================================搜索过程================================

                for (int i = 0; i < Crt_File.Length; i++)
                {
                    for (int j = 0; j < i; j++)
                        if ((Crt_File[i] < Crt_File[j]))
                        {
                            ca_name = FileName[i];
                            ca_time = Crt_File[i];

                            FileName[i] = FileName[j];
                            Crt_File[i] = Crt_File[j];

                            FileName[j] = ca_name;
                            Crt_File[j] = ca_time;

                        }

                }

                if (Crt_File.Length == 0)
                    num = -1;
                //===================================删除过程===============================
                if (num == -1)
                {
                    //report.Text += "\n磁盘空间警报！";
                    return;
                }
                else
                {
                    num = 0;
                    while (drive.AvailableFreeSpace < need && num < FileName.Length)
                    {
                        System.Console.WriteLine("移除文件:" + FileName[num]+"\n");

                        try
                        {
                            if (Directory.Exists(FileName[num]))//删除文件夹
                            {
                                Directory.Delete(FileName[num], true);
                            }
                            if (File.Exists(FileName[num]))//删除文件
                            {
                                File.Delete(FileName[num]);
                            }
                        }
                        catch (Exception ex)
                        {
                            //report.Text += ex.ToString();
                        }
                        num++;
                    }
                }
                free = drive.AvailableFreeSpace;
            }
        }

        public long IsNumeric(string str)
        {
            long i;
            if (str != null && System.Text.RegularExpressions.Regex.IsMatch(str, @"^-?\d+$"))
                i = long.Parse(str);
            else
                i = -1;
            return i;
        }
    }
}
